<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_User_entity{
	var $id = 0;
	var $username ="";
	var $email = "";
	var $password = "";
	var $full_name = "";
	var $city = "";
	var $state = "";
	var $country = "";
	var $country_code = "";
	var $mobile = "";
	var $created_time = 0;
	var $updated_time = 0;
	var $coin = 0;
	var $bonus = 0;
	var $cash = 0;
	var $referral_code = "";
	var $status = "";
	var $currency_code = "";
	var $avatar = "";
	var $email_subscription = "";
	var $sms_subscription = "";
	var $bonus_info = "";
	var $cod_mobile = "";
	var $cod_address = "";
	var $cod_status = "";
	var $cod_city = "";
	var $cod_state = "";
	var $cod_postalcode = "";
	var $sid = "";
	var $login_ip = '';
	var $user_reffer_id = 0;
  
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_USER)->result();
		}
		else if($cmd == 'username')
		{
			$result = $ci->db->where('username',$input)->get(TBL_USER)->result();
		}
		else if($cmd == 'email')
		{
			$result = $ci->db->where('email',$input)->get(TBL_USER)->result();
		}
		else if($cmd == 'referCode')
		{
			$result = $ci->db->where('referral_code',$input)->get(TBL_USER)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->username = $result[0]->username;
			$this->email = $result[0]->email;
			$this->password = $result[0]->password;
			$this->full_name = $result[0]->full_name;
			$this->city = $result[0]->city;
			$this->state = $result[0]->state;
			$this->country = $result[0]->country;
			$this->country_code = $result[0]->country_code;
			$this->mobile = $result[0]->mobile;
			$this->created_time = $result[0]->created_time;
			$this->updated_time = $result[0]->updated_time;
			$this->coin = $result[0]->coin;
			$this->bonus = $result[0]->bonus;
			$this->cash = $result[0]->cash;
			$this->referral_code = $result[0]->referral_code;
			$this->status = $result[0]->status;
			$this->currency_code = $result[0]->currency_code;
			$this->avatar = $result[0]->avatar;
			$this->email_subscription = $result[0]->email_subscription;
			$this->sms_subscription = $result[0]->sms_subscription;
			$this->bonus_info = $result[0]->bonus_info;
			$this->cod_mobile = $result[0]->cod_mobile;
			$this->cod_address = $result[0]->cod_address;
			$this->cod_status = $result[0]->cod_status;
			$this->cod_city = $result[0]->cod_city;
			$this->cod_state = $result[0]->cod_state;
			$this->cod_postalcode = $result[0]->cod_postalcode;
			$this->sid = $result[0]->sid;
			$this->login_ip = $result[0]->login_ip;
			$this->user_reffer_id = $result[0]->user_reffer_id;
      
			// Create user folder if not exists
			$directory = "./uploads/default/users";
			if(!file_exists($directory)){
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			
			$directory = "./uploads/default/users/".$result[0]->id ;
			if (!file_exists ( $directory )) {
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			return $this;
		}
		return false;
	}
	
	function setUserName($input){ $this->username=$input; }
	function setEmail($input){ $this->email=$input; }
	function setPassword($input){ $this->password=$input; }
	function setFullName($input){ $this->full_name=$input; }
	function setCity($input){ $this->city=$input; }
	function setState($input){ $this->state = $input; }
	function setCountry($input){ $this->country=$input; }
	function setCountryCode($input){ $this->country_code=$input; }
	function setMobile($input){ $this->mobile=$input; }
	function setCreatedTime($input){ $this->created_time=$input; }
	function setUpdatedTime($input){ $this->updated_time=$input; }
	function setCoin($input){ $this->coin=$input; }
	function setBonus($input){ $this->bonus=$input;}
	function setCash($input){ $this->cash=$input; }
	function setReferralCode($input){ $this->referral_code=$input; }
	function setStatus($input){ $this->status=$input; }
	function setCurrencyCode($input){ $this->currency_code= $input;}
	function setAvatar($input){ $this->avatar = $input; }
	function setEmailSubscription($input){$this->email_subscription = $input;}
	function setSmsSubscription($input){$this->sms_subscription = $input;}
	function setBonusInfo($input){$this->bonus_info = $input;}
	function setCodMobile($input){$this->cod_mobile = $input;}
	function setCodAddress($input){$this->cod_address = $input;}
	function setCodStatus($input){$this->cod_status = $input;}
	function setCodCity($input){ $this->cod_city=$input; }
	function setCodState($input){ $this->cod_state=$input; }
	function setCodePostalCode($input){ $this->cod_postalcode=$input; }
	function setSid($input){$this->sid=$input;}
	function setLoginIp($input){ $this->login_ip=$input;}
	function setUserRefferId($input){$this->user_reffer_id = $input;}
			
	function getId(){ return $this->id; }
	function getEmail(){ return $this->email; }
	function getPasswd(){ return $this->password; }
	function getUserName() { return $this->username;}
	function getFullName() { return $this->full_name;}
	function getCity(){ return $this->city;}
	function getState(){ return $this->state;}
	function getCountry(){ return $this->country; }	
	function getCountryCode(){ return $this->country_code; }
	function getMobile(){ return $this->mobile; }
	function getCreatedTime(){ return $this->created_time; }
	function getUpdatedTime(){ return $this->updated_time; }
	function getCoin(){ return $this->coin; }
	function getBonus(){ return $this->bonus; }
	function getCash(){ return $this->cash;}	
	function getReferralCode(){ return $this->referral_code; }
	function getStatus(){ return $this->status; }
	function getCurrencyCode(){ return $this->currency_code; }
	function getAvatar(){ return $this->avatar; }
	function getEmailSubscription(){return $this->email_subscription; }
	function getSmsSubscription(){ return $this->sms_subscription; }
	function getBonusInfo(){ return $this->bonus_info; }
	function getCodMobile(){ return $this->cod_mobile; }
	function getCodAddress(){ return $this->cod_address; }
	function getCodStatus(){ return $this->cod_status; }
	function getCodCity(){ return $this->cod_city; }
	function getCodState(){ return $this->cod_state; }
	function getCodePostalCode(){ return $this->cod_postalcode; }
	function getSid(){ return $this->sid;}
    function getLoginIp(){ return $this->login_ip;}
	function getUserRefferId(){ return $this->user_reffer_id ;}
	
	function getFolderFileStorage(){ 
		return "./uploads/default/users/".$this->id."/"; 
	}
	
	function getMyAvatar(){
		if($this->getAvatar())
			return $this->getFolderFileStorage().$this->getAvatar();
		else
			return site_url().'addons/default/themes/clicxa/css/images/avatar.jpg';
	}
	
	function checkPassword($string)
	{
		if($string == $this->password)
		{
			return true;
		}
		return false;
	}
	
	function update()
	{
		$ci=& get_instance();
		
		if($this->password != "")
		{
			$ci->db->set('password',$this->password);
		}
		$ci->db->set('email', $this->email);
		$ci->db->set('username', $this->username);
		$ci->db->set('full_name', $this->full_name);
		$ci->db->set('city', $this->city);
		$ci->db->set('state', $this->state);
		$ci->db->set('country', $this->country);
		$ci->db->set('country_code', $this->country_code);
		$ci->db->set('mobile', $this->mobile);
		$ci->db->set('created_time', $this->created_time);
		$ci->db->set('updated_time', $this->updated_time);
		$ci->db->set('coin', $this->coin);
		$ci->db->set('bonus', $this->bonus);
		$ci->db->set('cash', $this->cash);
		$ci->db->set('referral_code', $this->referral_code);
		$ci->db->set('status', $this->status);
		$ci->db->set('currency_code', strtoupper( $this->currency_code) );
		$ci->db->set('avatar', $this->avatar);
		$ci->db->set('email_subscription', $this->email_subscription);
		$ci->db->set('sms_subscription', $this->sms_subscription);
		$ci->db->set('bonus_info', $this->bonus_info);
		$ci->db->set('cod_mobile', $this->cod_mobile);
		$ci->db->set('cod_address', $this->cod_address);
		$ci->db->set('cod_status', $this->cod_status);
		$ci->db->set('cod_city', $this->cod_city);
		$ci->db->set('cod_state', $this->cod_state);
		$ci->db->set('cod_postalcode', $this->cod_postalcode);
		$ci->db->set('sid', $this->sid);
		$ci->db->set('login_ip', $this->login_ip);
		$ci->db->set('user_reffer_id', $this->user_reffer_id);
   
		$ci->db->where('id', $this->id)->update(TBL_USER);
	}
	
	function createNewUserEntity($username){
		$ci=& get_instance();
		
		$ci->db->set('username',$username);
		$ci->db->insert(TBL_USER);
		
		return $ci->db->insert_id();
	}
	
	function getArrayListOfUser(){
		$ci=& get_instance();
		return $ci->db->get(TBL_USER)->result();
	}
	
	function convertToUSD(){
		return changeValueOfMoney( $this->getCash(), 'usd', $this->getCurrencyCode() );
	}
	
	function getCountryCode_($country=''){
		$ci=& get_instance();
		$rs = $ci->db->where('country_name',$country)->get('default_world_countries')->result();
		if($rs){
			return $rs[0]->country_code;
		}
		return '';
	}
	
	function getCountryName($country_code){
		$ci=& get_instance();
		$code = substr(strtoupper($country_code),0,2);
		$rs = $ci->db->where('country_code',$code)->get('default_world_countries')->result();
		if($rs){
			return $rs[0]->country_name;
		}
		return '';
	}
	
	//
}
